<?php

/*****
* File :    .php
* module :  
* Created:  
* Update:   
* Function: 
* Author:   Eugene Heriniaina
* Email:    hery@serasera.org
* Comment:  
*******/

//manage status


switch($task) {
	case 'orderup':
		status_order( $id, -1, $option );
		break;
	case 'orderdown':
		status_order( $id, 1, $option );
		break;
	case 'new':
		status_edit( $option );
		break;
	case 'edit':
		status_edit( $option, $id );
		break;
	case 'save':
		status_save( $option, $id );
		break;
	case 'delete';
		status_delete( $option, $id );
		break;
	default:
		if($id) {
			status_view( $option, $id );
		} else {
			status_list( $option );
		}
		break;
}

function status_delete(  $option, $id ) {
	global $database, $access;
	if(!$access->canEdit) {
		mosNotAuth();
		return;
	}
	$row = new congregationStatus( $database );
	$row->load( $id );

	if(!$row->id) {
		echo "<script> alert('". _Status_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "SELECT count(*) FROM #__congregation_profile WHERE status='$row->id'";
	$database->setQuery( $query );

	$status = $database->loadResult();

	if($status > 0 ) {
		echo "<script> alert('". _Status_Has_Profiles ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "DELETE FROM #__congregation_status "
	. "\n WHERE id = '$id' "
	;
	$database->setQuery( $query );
	if (!$database->query()) {
		echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
	}
	mosRedirect( sefRelToAbs("index.php?option=$option&rub=status&Itemid=$Itemid"));
	
}


function status_view( $option, $id ) {
	global $my, $access, $database;
	if(!$access->canEdit || !$my->username) {
		mosNotAuth();
		return;
	}

	$row = new congregationStatus( $database );
	$row->load( $id );


	HTML_congregation::status_view( $row, $option );
}

/**
* Moves the order of a record
* @param integer The increment to reorder by
*/
function status_order( $uid, $inc, $option ) {
	global $database, $option, $rub, $Itemid;

	$row = new congregationStatus( $database );
	$row->load( $uid );
	$row->move( $inc );

	mosRedirect( sefRelToAbs("index.php?option=$option&rub=$rub&Itemid=$Itemid") );
}

function status_list( $option ) {
	global $database;

//for listing
	$limit 		= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
	$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );

	$query = "SELECT * FROM #__congregation_status ";

	$database->setQuery( $query );
	$total = $database->loadResult();
	
	if ( $total <= $limit ) $limitstart = 0;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );

	$pageNav = new mosPageNav( $total, $limitstart, $limit  );

	$query = "SELECT * FROM #__congregation_status ORDER BY ordering "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;
	
	$database->setQuery( $query );
	$rows =  $database->loadObjectList();

	HTML_congregation::status_list( $rows, $option, $pageNav );

}
function status_edit( $option, $id = null) {
	global $my, $access, $database;

	if( !$access->canEdit ) {
		mosNotAuth();
		return;
	}

	$row = new congregationStatus( $database );
	$row->load( $id );

	$lists = array();

	// build the html select list for ordering
	$query = "SELECT ordering AS value, text "
	. "\n FROM #__congregation_status"
	. "\n ORDER BY ordering "
	;
	$lists['ordering'] 		= mosAdminMenus::SpecificOrdering( $row, $id, $query, 1 );

  HTML_congregation::status_edit( $row, $option, $lists );
}

function status_save( $option, $id ) {
	global $Itemid, $rub, $database;

	$row = new congregationStatus( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	
	mosRedirect( sefRelToAbs("index.php?option=$option&rub=$rub&Itemid=$Itemid"), "Saved" );


}


?>